package kr.hkit.androidcomplete02.ch12;

import java.util.ArrayList;

import kr.hkit.androidcomplete02.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyCustomAdapter extends BaseAdapter {
	String TAG="MyListAdapter";
	LayoutInflater inflater;
	Context mContext;
	ArrayList<MyMenu> arLists;
	int layoutRes;
	
	public MyCustomAdapter(Context context, int aResLayout, ArrayList<MyMenu> lists) {
		mContext = context;
		arLists = lists;
		layoutRes = aResLayout;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		Log.d(TAG, "getCount()");
		return arLists.size();
	}

	public Object getItem(int position) {
		Log.d(TAG, "getItem(" + position +")");
		return arLists.get(position);
	}

	public long getItemId(int position) {
		Log.d(TAG, "getItemId(" + position +")");
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d(TAG, "getView(" + position +")");
		
		final int pos = position;
		
		if(convertView == null){
			convertView = inflater.inflate(layoutRes, parent, false);
		}
		
		ImageView img = (ImageView) convertView.findViewById(R.id.ch12_img);
		img.setImageResource(arLists.get(position).iconRes);
		
		TextView tv = (TextView) convertView.findViewById(R.id.ch12_text);
		tv.setText(arLists.get(position).name);
		
		Button btn = (Button) convertView.findViewById(R.id.ch12_btn);
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String text = arLists.get(pos).name + "를 주문합니다.";
				Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
			}
		});
		
		return convertView;
	}

}
